﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Web.Mvc;
using DataAccess.DB;
using DuocThaiBinh.Controllers;

namespace DuocThaiBinh.WebUI.HtmlHelpers
{
    public static class HtmlHelpers
    {
        public static string ActivePage(this HtmlHelper helper, string controller)
        {
            string classValue = "";

            string currentController = helper.ViewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString();

            if (controller.Contains(string.Format(",{0},",currentController)))
            {
                classValue = "current";
            }

            return classValue;
        }

        public static MvcHtmlString SelectCuaHang(this HtmlHelper helper)
        {
            if (!WebMatrix.WebData.WebSecurity.IsAuthenticated)
            {
                return MvcHtmlString.Create(string.Empty);
            }

            StringBuilder retVal = new StringBuilder();          

            UserInCuaHangController userInKhoController = new UserInCuaHangController();
            List<CuaHang> cuaHangs = userInKhoController.GetCuaHangsByUserName(helper.ViewContext.HttpContext.User.Identity.Name);
            int currentCuaHangID = Convert.ToInt32(System.Web.HttpContext.Current.Session["CuaHangID"]);
            CuaHang currentCuaHang = cuaHangs.Where(ch => ch.ID == currentCuaHangID).FirstOrDefault();
            if (currentCuaHang == null)
            {
                retVal.AppendLine("<a class=\"dropdown-toggle\" data-toggle=\"dropdown\" href=\"#\">");
                retVal.AppendLine("<i class=\"fa fa-tasks fa-fw\"></i> Chưa chọn cửa hàng <i class=\"fa fa-caret-down\"></i>");
                retVal.AppendLine("</a>");
            }
            else
            {
                retVal.AppendLine("<a class=\"dropdown-toggle\" data-toggle=\"dropdown\" href=\"#\">");
                retVal.AppendLine("<i class=\"fa fa-tasks fa-fw\"></i> " + currentCuaHang.TenCuaHang + " <i class=\"fa fa-caret-down\"></i>");
                retVal.AppendLine("</a>");
            }

            retVal.AppendLine("<ul class=\"dropdown-menu dropdown-user\">");

            foreach (var cuaHang in cuaHangs)
            {
                if (currentCuaHangID != cuaHang.ID)
                {
                    retVal.AppendLine("<li>");
                    retVal.AppendLine("<a href=\"javascript:void(0);\" onclick=\"ChangeKhoFunc(" + cuaHang.ID + ")\">");
                    retVal.AppendLine("<div>");
                    retVal.AppendLine("<nobr><i class=\"fa fa-tasks fa-fw\"></i> " + cuaHang.TenCuaHang);
                    retVal.AppendLine("</nobr></div>");
                    retVal.AppendLine("</a>");
                    retVal.AppendLine("</li>");
                    retVal.AppendLine("<li class=\"divider\"></li>");
                }
            }

            retVal.AppendLine("</ul>");
            return MvcHtmlString.Create(retVal.ToString());
        }
    }
}